<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Car;
use App\Models\User;
use Session;
use App\Services\RedisService;

class CarController extends Controller
{
    public function show(Request $request)
    {
    	$rs['status'] = true;
    	$id           = (int)$request->car_id;
    	$rs['data']   = Car::with('type')->find($id);
    	return response()->json($rs);
    	
    }

    public function getCars(Request $request)
    {
        $lat = $request->lat? :'-37.6122';
        $lng = $request->lng? :'141.9131';
        $cars = RedisService::geoFindCar($lng,$lat,10000000);
        //dd($cars);
        $car_arr = [];
        foreach ($cars as $k => $v) {
        	$data = Car::with('type')->find($v[0]);
            if($data) $data = $data->toArray();
            if($data['status'] != 'available') continue;
        	$data['distance'] = round($v[1],1).'m';
        	array_push($car_arr, $data);
        }

        $rs['status'] = true;
        $rs['data']   = $car_arr;
        return response()->json($rs);
       	

    }



    
}
